package com.yundasys.lab08jvm.commonspool;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Handler;

/**
 * @PACKAGE_NAME: com.yundasys.lab08jvm.commonspool
 * @NAME: DataSourceEndpoint
 * @USER: jiangta
 * @DATE: 2021/2/11
 * @TIME: 11:31 下午
 * @PROJECT_NAME: springboot-labs
 */
@Endpoint(id = "datasource")
public class DataSourceEndpoint {

    private DMDataSource dataSource;

    public DataSourceEndpoint(DMDataSource dataSource) {
        this.dataSource = dataSource;
    }

    @ReadOperation
    public Map<String,Object> pool() {
        GenericObjectPool<MyConnection> pool = dataSource.getPool();
        HashMap<String,Object>  map = new HashMap<>();
        map.put("numActive",pool.getNumActive());
        map.put("numIdle",pool.getNumIdle());
        map.put("createdCount",pool.getCreatedCount());
        return map;
    }
}
